package com.baizhi.action;

import com.baizhi.util.JDBCUtil;
import com.baizhi.entity.Title;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@WebServlet("/deleteArticleServlet")
public class DeletBlogServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException {

        doGet(request, response);
    }

        protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //获取标题
        int id= Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        PreparedStatement pstmt=null;
        Connection conn = null;
        String sql;
            try {
                conn = JDBCUtil.getConnection();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
            //获取连接


            if(id==-1){
                sql="delete from title ";

            }else {
                sql="delete from title where id= "+id;
            }
            //执行sql语句
            pstmt=conn.prepareStatement(sql);
            pstmt.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();

        } finally {
                try {
                    if (pstmt != null) {
                        pstmt.close();
                    }
                    pstmt = null;
                    if (conn != null) {
                        conn.close();
                    }
                    conn = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
        }

        request.getRequestDispatcher("blog.jsp").forward(request, response);
    }

}